<template>
  <el-sub-menu index="10">
    <template #title>{{ currentUserData.username }}</template>
    <el-menu-item @click="handleLogout(currentUserData)" index="10-1">退出登录</el-menu-item>
  </el-sub-menu>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getLogoutHttp, getUsersHttp } from '@/api/request/index.js'
import { useRoute, useRouter } from 'vue-router'

/* 实例化路由对象 */
const route = useRoute()
const router = useRouter()
const userType = route.query.type
const userName = route.query.username
// console.log(userType) // 打印 登录用户类型
// console.log(userName) // 打印 登录用户名
/* 定义登录用户类型 的所有用户信息 */
const userData = ref([])
/* 定义当前登录用户的详细信息 */
const currentUserData = ref({})

/* 退出登录方法 */
const handleLogout = async (data) => {
  let logoutRes = await getLogoutHttp(data)
  let logoutMsg = logoutRes.data.msg
  console.log(logoutMsg)
  router.back()
}

onMounted(async () => {
  const res = await getUsersHttp({
    type: userType,
  })
  userData.value = res.data.result
  // console.log(userData.value) // 打印 登录用户类型的所有用户信息
  /* 查找当前用户的详细信息 */
  currentUserData.value = userData.value.find(
    (item) => item.username === userName
  )
  console.log('当前登录用户', currentUserData.value) // 打印 当前登录用户的详细信息
})
</script>

<style scoped></style>
